﻿using System;
using System.Collections.Generic;
using ProtoBuf.Message;
using UnityEngine;

namespace UI
{
    public class ChatMessageCenter
    {
        public const int CHAT_WORLD = 1;    //世界
        public const int CHAT_UNION = 2;    //公会
        public const int CHAT_PRIVATE = 3;  //私聊
        public const int CHAT_TEAM = 4;     //队伍
        public const int CHAT_SCENE = 5;    //野外

        public List<SCChatTcp>[] messages = new List<SCChatTcp>[5];
        public int[] MAX_MESSAGE_COUNT = { 20, 20, 20, 20, 20 };
        private Action<int, SCChatTcp> getMessageHandler;
        public ChatMessageCenter()
        {
            for (int i = 0; i < messages.Length; ++i)
            {
                messages[i] = new List<SCChatTcp>();
            }
        }
        public void Clear()
        {
            for (int i = 0; i < messages.Length; ++i)
            {
                messages[i].Clear();
            }
        }

        public void AddMessage(SCChatTcp msg)
        {
            if(msg.chatType >= messages.Length || msg.chatType < 1)
            {
                Debug.LogError("Message Type Error");
                return;
            }
            var ms = messages[msg.chatType - 1];
            if(ms.Count >= MAX_MESSAGE_COUNT[msg.chatType - 1])
            {
                ms.RemoveAt(0);
            }
            ms.Add(msg);
            if(getMessageHandler != null)
            {
                getMessageHandler(msg.chatType, msg);
            }
        } 

        public void AddMessageHandler(Action<int, SCChatTcp> handler)
        {
            getMessageHandler += handler;
        }

        public void RemoveMessageHandler(Action<int, SCChatTcp> handler)
        {
            if(getMessageHandler != null)
            {
                getMessageHandler -= handler;
            }
        }

    }
}
